package cn.eng.org.controller;

import cn.eng.basic.util.AjaxResult;
import cn.eng.basic.util.PageList;
import cn.eng.org.domain.Employee;
import cn.eng.org.query.EmployeeQuery;
import cn.eng.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口
 */
@RestController //@ResponseBody+Controller 组合注解
@RequestMapping("/employee")// 请求地址
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 接口:查询单个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id")  Long id){
        return employeeService.loadById(id);
    }

    /**
     * 接口:查询所有
     * @return
     */
    @GetMapping
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
     * 接口: 分页查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
      return employeeService.queryPage(employeeQuery);
    }

    /**
     * 接口 删除对象接口
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return new AjaxResult();
         } catch (Exception e) {
         e.printStackTrace();
         return new AjaxResult(false,"删除失败");
         }
    }

    /**
     * 接口: 添加或者修改接口
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult addOrupdate(@RequestBody Employee employee){
        try {
            //判断id ,无id 执行添加, 有id 执行修改
            if(employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
         } catch (Exception e) {
         e.printStackTrace();
         return new AjaxResult(false,"操作失败");
         }
    }
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
            return employeeService.list(shopId);
    }
}
